package org.csu.myblogger.myblogger.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.csu.myblogger.myblogger.commom.CommonResponce;
import org.csu.myblogger.myblogger.commom.JwtTokenUtil;
import org.csu.myblogger.myblogger.entity.Article;
import org.csu.myblogger.myblogger.service.service.AccountService;
import org.csu.myblogger.myblogger.service.service.ArticleService;
import org.csu.myblogger.myblogger.service.service.CommentService;
import org.csu.myblogger.myblogger.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/article/")
public class ArticleController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @PostMapping("PublishArticle/{username}")
    public CommonResponce PublishArticle(@PathVariable String username, @RequestHeader("Authorization")String toekn, @RequestBody ArticleVo articleVo){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(toekn);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"无权操作当前用户");
            }
            return articleService.PublishArticle(username,articleVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @PutMapping("EditArticle/{username}/{title}")
    public CommonResponce EditArticle(@PathVariable String username, @PathVariable String title,
                                      @RequestHeader("Authorization")String toekn, @RequestBody ArticleVo articleVo){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(toekn);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"无权操作当前用户");
            }
            return articleService.EditArticle(username,title,articleVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @DeleteMapping("DeleteArticle/{username}")
    public CommonResponce DeleteArticle(@PathVariable String username, @RequestHeader("Authorization")String toekn,
                                        @RequestBody ArticleVo articleVo){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(toekn);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"无权操作当前用户");
            }
            return articleService.DeleteArticle(username,articleVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }

    @GetMapping("ArticleClassification/{username}")
    public CommonResponce ArticleClassification(@PathVariable String username,
                                                @RequestHeader("Authorization")String toekn, @RequestBody ArticleVo articleVo){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(toekn);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"无权操作当前用户");
            }
            return articleService.ArticleClassification(username,articleVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    //收藏
    @GetMapping("ArticleBookmark/{username}")
    public CommonResponce ArticleBookmark(@PathVariable String username, @RequestHeader("Authorization")String toekn, @RequestBody ArticleVo articleVo){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(toekn);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"无权操作当前用户");
            }
            return articleService.ArticleBookmark(username,articleVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @GetMapping("CancleArticleBookmark/{username}")
    public CommonResponce CancleArticleBookmark(@PathVariable String username, @RequestHeader("Authorization")String toekn, @RequestBody ArticleVo articleVo){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(toekn);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"无权操作当前用户");
            }
            return articleService.CancleArticleBookmark(username,articleVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @GetMapping("SearchArticle/{articleid}")
    public CommonResponce CancleArticleBookmark(@PathVariable String articleid){
       return articleService.SearchArticle(articleid);
    }
}
